<template>
  <el-dialog
    title="审批"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="登录帐号" readonly="true"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" >
        <el-input v-model="dataForm.email" placeholder="邮箱" readonly="true"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="dataForm.mobile" placeholder="手机号" readonly="true"></el-input>
      </el-form-item>
      <el-form-item label="公司名称" >
        <el-input v-model="custInfo.companyName" placeholder="公司名称" readonly="true"></el-input>
      </el-form-item>
      <el-form-item label="接入点数" >
        <el-input v-model="dataForm.amount" placeholder="接入点数" ></el-input>
      </el-form-item>
      <el-form-item label="角色" size="mini" prop="roleIdList">
        <el-checkbox-group v-model="dataForm.roleIdList">
          <el-checkbox v-for="role in roleList" :key="role.roleId" :label="role.roleId">{{ role.roleName }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="关联的分表">
        <el-select v-model="dataForm.extendId"  placeholder="请选择">
          <el-option
            v-for="item in extendInfos"
            :key="item.extendId"
            :label="item.extendName"
            :value="item.extendId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">通过</el-radio>
          <el-radio :label="2">拒绝</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span id="footer" slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isEmail, isMobile } from '@/utils/validate'
  export default {
    data () {
      var validatePassword = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('密码不能为空'))
        } else {
          callback()
        }
      }
      var validateComfirmPassword = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('确认密码不能为空'))
        } else if (this.dataForm.password !== value) {
          callback(new Error('确认密码与密码输入不一致'))
        } else {
          callback()
        }
      }
      var validateEmail = (rule, value, callback) => {
        if (!isEmail(value)) {
          callback(new Error('邮箱格式错误'))
        } else {
          callback()
        }
      }
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        custInfo:{},
        extendInfos:[],
        visible: false,
        roleList: [],
        projectInfoList:[],
        dataForm: {
          id: 0,
          userName: '',
          password: '',
          comfirmPassword: '',
          salt: '',
          email: '',
          mobile: '',
          roleIdList: [],
          status: '1',
          amount: '',
          extendId:'',
          projecIds:[],
          customerId:''

        },
        dataRule: {
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
          comfirmPassword: [
            { validator: validateComfirmPassword, trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' },
            { validator: validateEmail, trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, projectInfoList,info) {
        this.projectInfoList =  projectInfoList
        this.dataForm.id = id || 0

        this.$api.okhttp({
          url: this.$http.adornUrl('/sys/extend/'+info.customerId),
          method: 'get',
          params: this.$http.adornParams()
        }).then((data) => {
          this.extendInfos = data && data.code === 0 ? data.info:[];
          this.dataForm.extendId = data.extend&&data.extend!='' ?parseInt(data.extend):''
        this.$api.okhttp({
          url: this.$http.adornUrl('/sys/custInfo/'+info.customerId),
          method: 'get',
          params: this.$http.adornParams()
        }).then((data) => {
         this.custInfo = data && data.code === 0 ? data.info:{};
         this.dataForm.amount = data.info.amount
         this.$api.okhttp({
          url: this.$http.adornUrl('/sys/role/select'),
          method: 'get',
          params: this.$http.adornParams()
        }).then((data) => {
          this.roleList = data && data.code === 0 ? data.list : []
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (this.dataForm.id) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/sys/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.user.username
                this.dataForm.salt = data.user.salt
                this.dataForm.email = data.user.email
                this.dataForm.mobile = data.user.mobile
                this.dataForm.roleIdList = data.user.roleIdList
                this.dataForm.status = data.user.status
                this.dataForm.projecIds = data.user.projectIdList
                this.dataForm.customerId = data.user.customerId
                if(data.user.status == '1' || data.user.status == '2' )
                {
                  document.getElementById('footer').style.display='none';
                }
                else
                {
                  document.getElementById('footer').style.display='block';
                }
              }
            })
          }
        })
      })
      })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/sys/user/${!this.dataForm.id ? 'save' : 'audit'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.id || undefined,
                'username': this.dataForm.userName,
                'password': this.dataForm.password,
                'salt': this.dataForm.salt,
                'email': this.dataForm.email,
                'mobile': this.dataForm.mobile,
                'status': this.dataForm.status,
                'roleIdList': this.dataForm.roleIdList,
                'projectIdList': this.dataForm.projecIds,
                'extendId': this.dataForm.extendId,
                "customerId":this.dataForm.customerId,
                "amount":this.dataForm.amount
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
